package com.ambulance.project.manager.service.impl;

import com.ambulance.common.exception.CustomException;
import com.ambulance.common.utils.SecurityUtils;
import com.ambulance.project.manager.domain.BizDepart;
import com.ambulance.project.manager.domain.BizNurse;
import com.ambulance.project.manager.mapper.BizDepartMapper;
import com.ambulance.project.manager.mapper.BizNurseMapper;
import com.ambulance.project.manager.service.IBizNurseService;
import com.ambulance.project.manager.utils.MgtCommonUtil;
import com.ambulance.project.manager.utils.MgtUserLoginResult;
import com.ambulance.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 护士管理Service业务层处理
 *
 * @author zr
 * @date 2020-05-11
 */
@Service
public class BizNurseServiceImpl implements IBizNurseService {
    @Autowired
    private BizNurseMapper bizNurseMapper;
    @Autowired
    private MgtCommonUtil mgtCommonUtil;
    @Autowired
    private BizDepartMapper bizDepartMapper;

    /**
     * 查询护士管理
     *
     * @param nurseID 护士管理ID
     * @return 护士管理
     */
    @Override
    public BizNurse selectBizNurseById(Long nurseID) {
        return bizNurseMapper.selectBizNurseById(nurseID);
    }

    /**
     * 查询护士管理列表
     *
     * @param bizNurse 护士管理
     * @return 护士管理
     */
    @Override
    public List<BizNurse> selectBizNurseList(BizNurse bizNurse) {
        final MgtUserLoginResult mgtUserLoginResult = mgtCommonUtil.getMgtUserLoginResult();
        bizNurse.setUid(new Long(mgtUserLoginResult.getUid()));
        final List<BizNurse> bizNurses = bizNurseMapper.selectBizNurseList(bizNurse);
        if (!CommonUtils.checkNull(bizNurse.getNurseID())) {
            final BizNurse bizNurseById = selectBizNurseById(bizNurse.getNurseID());
            bizNurses.add(bizNurseById);
        }
        return bizNurses;
    }

    /**
     * 新增护士管理
     *
     * @param bizNurse 护士管理
     * @return 结果
     */
    @Override
    public int insertBizNurse(BizNurse bizNurse) {
        final MgtUserLoginResult mgtUserLoginResult = mgtCommonUtil.getMgtUserLoginResult();
        bizNurse.setUid(new Long(mgtUserLoginResult.getUid()));
        bizNurse.setHospital(mgtUserLoginResult.getHospitalName());
        final BizDepart bizDepart = bizDepartMapper.selectBizDepartById(new Long(bizNurse.getDepartId()));
        bizNurse.setDepartId(bizDepart.getId().intValue());
        bizNurse.setDepart(bizDepart.getDepart());
        bizNurse.setPassword(SecurityUtils.encryptPassword("123456"));
        return bizNurseMapper.insertBizNurse(bizNurse);
    }

    /**
     * 修改护士管理
     *
     * @param bizNurse 护士管理
     * @return 结果
     */
    @Override
    public int updateBizNurse(BizNurse bizNurse) {
        final BizDepart bizDepart = bizDepartMapper.selectBizDepartById(new Long(bizNurse.getDepartId()));
        bizNurse.setDepartId(bizDepart.getId().intValue());
        bizNurse.setDepart(bizDepart.getDepart());
        return bizNurseMapper.updateBizNurse(bizNurse);
    }

    /**
     * 批量删除护士管理
     *
     * @param nurseIDs 需要删除的护士管理ID
     * @return 结果
     */
    @Override
    public int deleteBizNurseByIds(Long[] nurseIDs) {
        for (Long item : nurseIDs) {
            final BizNurse bizNurse = selectBizNurseById(item);
            if (bizNurse.getClassStatus().equals(1)) {
                throw new CustomException("您尚在班组中,无法删除");
            }
        }
        return bizNurseMapper.deleteBizNurseByIds(nurseIDs);
    }

    /**
     * 删除护士管理信息
     *
     * @param nurseID 护士管理ID
     * @return 结果
     */
    @Override
    public int deleteBizNurseById(Long nurseID) {
        return bizNurseMapper.deleteBizNurseById(nurseID);
    }

    /**
     * 冻结护士
     *
     * @param nurseID 护士管理ID
     * @return
     */
    @Override
    public int lightBizNurse(Long nurseID) {
        final BizNurse bizNurse = bizNurseMapper.selectBizNurseById(nurseID);
        if (bizNurse.getAccountStatus().equals(1) && bizNurse.getStatus().equals(1)) {
            throw new CustomException("您的账号已冻结,请勿重复冻结");
        }
        return bizNurseMapper.lightBizNurseById(nurseID);
    }
}
